package com.homelink.crashhandle;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by Fcy on 2015/10/21.
 */
public class LimitArrayList<E> extends ArrayList<E>
{
    /**
     * 当前指针位置
     */
	private int index = -1;

    /**
     * 实际数量（包含覆盖的元素）
     */
	private int realIndex = -1;

    /**
     * 最多容纳元素个数
     */
	private int max = -1; // 不限制

	public void setMax(int max)
	{
		this.max = max;
	}

	public int getIndex()
	{
		return index;
	}

	public int getRealIndex()
	{
		return realIndex;
	}

	public int getMax()
	{
		return max;
	}

	@Override
	public void clear()
	{
		super.clear();
		index = -1;
		realIndex = -1;
	}

	@Override
	public boolean add(E object)
	{
		if (max > 0)
		{
			index = ++realIndex % max;
			if (realIndex >= max)
			{ // 填满了，从头开始，覆盖
				set(index, object);
				return true;
			}
		}
		return super.add(object);
	}

	@Override
	public void add(int index, E object)
	{
		if (max > 0)
			throw new IllegalArgumentException("Stub");
		super.add(index, object);
	}

	@Override
	public boolean addAll(Collection<? extends E> collection)
	{
		if (max > 0)
			throw new IllegalArgumentException("Stub");
		return super.addAll(collection);
	}

	@Override
	public boolean addAll(int index, Collection<? extends E> collection)
	{
		if (max > 0)
			throw new IllegalArgumentException("Stub");
		return super.addAll(index, collection);
	}

}
